package com.itheima.controller;


import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 报表
 */
@Api(tags = "ReportController-报表相关接口")
@RestController
@RequestMapping("/report")
public class ReportController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private SetmealService setmealService;


    @ApiOperation(value = "会员统计",notes = "会员统计")
    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        // 获取年月时间列表
        Map<String, Object> map = new HashMap<>();
        List<String> months = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);//2018-12-10
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);//2019-1-10
            months.add(new SimpleDateFormat("yyyy.MM").format(calendar.getTime()));
        }
        map.put("months",months);

        List<Integer> memberCount = memberService.findMemberCountByMonths(months);

        map.put("memberCount",memberCount);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);



    }

    //查询套餐预约占比数据统计
    @ApiOperation(value = "查询套餐预约占比数据统计",notes = "查询套餐预约占比数据统计")
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){

        //调用Service查询


        //结果处理后封装到map集合
        //Map map = new HashMap();
        //map.put("setmealNames",setmealNames);
        //map.put("setmealCount",setmealCount);

        // 返回结果:setmealNames  setmealCount
//        return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,null);

        List<Map<String,Object>> setmealCount = setmealService.findSetmealCount();

        List<String> setmealNames = new ArrayList<>();
        for (Map<String, Object> map : setmealCount) {
            String name = (String) map.get("name");
            setmealNames.add(name);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("setmealNames",setmealNames);

        return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,data);

    }

    @Autowired
    private ReportService reportService;

    //获取运营数据统计
    @ApiOperation(value = "获取运营数据统计",notes = "获取运营数据统计")
    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        Map<String,Object> map = null;
        try {
            map = reportService.getBusinessReport();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS, null);
    }

    //运营数据报表导出
    @ApiOperation(value = "运营数据报表导出",notes = "运营数据报表导出")
    @GetMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response){
        return null;
    }
}
